#ifndef ELSE_H_
#define ELSE_H_

# include "stdio.h"
# include "string.h"
# include "all_struct.h"
# include "FindAndLook.h"
# include "windows.h"

/*
	此部分函数由周珂宇完成
*/


//sort.h
/*static void sortformat_Airplane(Airplane* airplaneHead);
//static void sort_Airplane(Airplane* airplaneHead);*/

//fileformat.h
/*static void empty_LinkedList(Airplane* airplaneHead, Guest* guestHead);
//static void read_file(Airplane* airplaneHead, Guest* guestHead);
//static void save_byFile(Airplane* airplaneHead, Guest* guestHead);
//static void save_byFile_1(Airplane* airplaneHead, Guest* guestHead);*/

//other.h
//char * s_gets(char * st,int n);
//static void pre_selectair(N_air* airplaneHead, int y, int m, int d);
//static int Recommend(Airplane* p,Airplane* airplaneHead);


//释放所有申请内存的链表空间（销毁
static void empty_LinkedList(Airplane* airplaneHead,Guest* guestHead)
{
	Airplane* tool;
	while(airplaneHead)
	{
		tool = airplaneHead;
		airplaneHead = airplaneHead->next;
		free(tool);
		tool = NULL; //防野指针
	}

	Guest* tool2;
	while(guestHead)
	{
		tool2 = guestHead;
		guestHead = guestHead->next;
		free(tool2);
		tool2 = NULL;//防野指针
	}

//	fprintf (stdout,"success to empty the link \n");

}
//保存信息到文件 //csv （对外的安全规范展示可公开信息（admin
static void save_byFile_1(Airplane* airplaneHead,Guest* guestHead)
{
	FILE *fp_air, *fp_guest,*fp;

	Airplane *p1 = airplaneHead->next;
	Guest *p2 =  guestHead->next;
	Airplane *p3 =  airplaneHead->next ;

	if(p3 != NULL)
	{
		if((fp = fopen("guest_airplance.csv","w")) == NULL)  //打开文件
			fprintf(stderr,"\n无法打开文件guest_airplance.csv\n");

		else
		{
			fprintf (fp,"订单乘客昵称,订单乘客姓名,订单乘客电话,飞机号,起飞城市,抵达城市,航空公司,飞机型号,");
			fprintf (fp,"原价格,折扣,");
			fprintf (fp,"所在年,月,日,起飞时间（小时,起飞时间（分钟,抵达时间（小时,抵达时间（分钟\n");

			while(p3)
			{
				if(0 != p3 ->personNumber)
					for(int i = 0; i < p3 ->personNumber; i++)
					{
						fprintf (fp,"%s,%s,%s,%s,%s,%s,%s,%s,%d,%lf,%d,%d,%d,%d,%d,%d,%d",
						         p3->Guest[i].userName,p3->Guest[i].realName,p3->Guest[i].phone,
						         p3->flightNumber,p3->startCity,p3->endCity,
						         p3->company,p3->type,
						         p3->price,p3->discount,
						         p3->time.year,p3->time.month,p3->time.day,
						         p3->time.original_hour,p3->time.original_minute,
						         p3->time.end_hour,p3->time.end_minute
						        );

						fprintf (fp,"\n");
					}

				p3 = p3->next;
			}


		}
		fclose(fp); //关闭文件
	}

	if(p1 != NULL)
	{
		if((fp_air = fopen("airplance.csv","w")) == NULL)  //打开文件
			fprintf(stderr,"\n无法打开文件airplance.csv\n");

		else
		{
			fprintf (fp_air,"飞机号,起飞城市,抵达城市,航空公司,飞机型号,");
			fprintf (fp_air,"原价格,折扣,总票量,剩余票数,订票人数,");
			fprintf (fp_air,"所在年,月,日,起飞时间（小时,起飞时间（分钟,抵达时间（小时,抵达时间（分钟\n");

			while(p1)
			{
				//fwrite(p1,sizeof(Airplane),1,fp_air);
				fprintf (fp_air,"%s,%s,%s,%s,%s,%d,%lf,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
				         p1->flightNumber,p1->startCity,p1->endCity,p1->company,p1->type,
				         p1->price,p1->discount,p1->ticketQuantity,p1->remainQuantity,p1->personNumber,
				         p1->time.year,p1->time.month,p1->time.day,
				         p1->time.original_hour,p1->time.original_minute,
				         p1->time.end_hour,p1->time.end_minute
				        );


				fprintf (fp_air,"\n");

				p1 = p1->next;
			}
		}

		fclose(fp_air); //关闭文件
	}

	if(p2 != NULL)
	{
		if((fp_guest = fopen("passenger.csv","w")) == NULL)  //打开文件
			fprintf(stderr,"\n无法打开文件passenger.csv\n");

		else
		{

			fprintf (fp_guest,"用户名,密码,身份证,姓名,电话,账户余额,订票数\n");

			while(p2)
			{
				//fwrite(p2,sizeof(Guest),1,fp_guest);
				fprintf (fp_guest,"%s,%s,%s,%s,%s,%lf,%d",
				         p2->userName,p2->password,p2->passcard,
				         p2->realName,p2->phone,
				         p2->money,p2->airnum
				        );


				fprintf (fp_air,"\n");
				p2 = p2->next;
			}

		}
		fclose(fp_guest); //关闭文件
	}

	printf("信息保存成功！");
	system("pause");

}
//保存信息到文件 //二进制 （对内的私密、保密性处理
static void save_byFile(Airplane* airplaneHead,Guest* guestHead)
{
	FILE *fp_air, *fp_guest;

	Airplane *p1 = airplaneHead->next;
	Guest *p2 =  guestHead->next;

	if(p1 != NULL)
	{
		if(NULL == (fp_air = fopen("airplance.txt","wb+")) )  //打开文件
			fprintf(stderr,"\n无法打开文件airplance.txt\n");

		else
		{
			while(p1)
			{
				fwrite(p1,sizeof(Airplane),1,fp_air);
				p1 = p1->next;
			}
		}

		fclose(fp_air); //关闭文件
	}

	if(p2 != NULL)
	{
		if(NULL == (fp_guest = fopen("passenger.txt","wb+")) )  //打开文件
			fprintf(stderr,"\n无法打开文件passenger.txt\n");

		else
		{
			while(p2)
			{
				fwrite(p2,sizeof(Guest),1,fp_guest);
				p2 = p2->next;
			}
		}
		fclose(fp_guest); //关闭文件
	}

//	printf("信息保存成功！");
//	system("pause");

}
//读取二进制文件信息
static void read_file(Airplane* airplaneHead,Guest* guestHead)
{
	FILE *fp_air, *fp_guest;

	//fp_air
	if(NULL != (fp_air = fopen("airplance.txt","rb+")) )
	{
		fgetc(fp_air);
		if(feof(fp_air) != 0) return;//防止EOF读入。

		fseek(fp_air,-1L,1);
		while(feof(fp_air) == 0)
		{
			Airplane *p = (Airplane *) malloc (sizeof(Airplane));

			fread(p,sizeof(Airplane),1,fp_air);//将一节点大小数据赋值给对应p指针只想地址块
			p->next = airplaneHead->next;//头插法
			airplaneHead->next = p;

			fgetc(fp_air);
			if(feof(fp_air) != 0)
				break;//当遇到EOF，fp前移一位
			else
				fseek(fp_air,-1L,1);  //确保不会把EOF相关读入,当fp下一字符为EOF就结束
		}
	}
	

	//fp_guest
	if(NULL != (fp_guest = fopen("passenger.txt","rb+")) )
	{
		fgetc(fp_guest);
		if(feof(fp_guest) != 0) return; //防止EOF读入。

		fseek(fp_guest,-1L,1);
		while(feof(fp_guest) == 0)
		{
			Guest *p = (Guest *) malloc (sizeof(Guest));

			fread(p,sizeof(Guest),1,fp_guest); ///将一节点大小数据赋值给对应p指针只想地址块
			p->next = guestHead->next; //头插法
			guestHead->next = p;

			fgetc(fp_guest);
			if(feof(fp_guest) != 0)
				break; //当遇到EOF，fp前移一位
			else
				fseek(fp_guest,-1L,1);  //确保不会把EOF相关读入,当fp下一字符为EOF就结束
		}
	}


	fclose(fp_air); //关闭文件fp_air
	fclose(fp_guest); //关闭文件fp_guest
}
//航班排序算法实现
static void sortformat_Airplane(Airplane* airplaneHead)
{
	Airplane *fr_pre = airplaneHead;
	Airplane *front = fr_pre->next;
	Airplane *be_pre = front;
	Airplane *behind = be_pre->next;

	while(front->next != NULL)
	{
		while(behind != NULL)
		{
			if(0 > strcmp(front->flightNumber, behind->flightNumber))
			{
				if(be_pre == front)
				{
					front->next = behind->next;
					fr_pre->next = behind;
					behind->next = front;
					be_pre = fr_pre->next;
				}

				else
				{
					Airplane *temp = front->next;
					be_pre->next = front;
					front->next = behind->next;
					fr_pre->next = behind;
					behind->next = temp;
				}
				behind = front;
				front = fr_pre->next;
			}
			be_pre = be_pre->next;
			behind = be_pre->next;
		}

		fr_pre = fr_pre->next;
		front = fr_pre->next;
		be_pre = front;
		behind = be_pre->next;
	}
}
//根据航班号排序
static void sort_Airplane(Airplane* airplaneHead,Guest* guestHead)
{
	if (NULL == airplaneHead->next)
	{
		fprintf (stdout,"无航班信息\n");
		return ;
	}

	sortformat_Airplane(airplaneHead);
	fprintf (stdout,"排序完成，结果如下：\n");
	find_AllAirplane(airplaneHead);

	save_byFile(airplaneHead,guestHead);
}
//确保时效性函数
static void pre_selectair(N_air* airplaneHead, int y, int m, int d)
{
	if (airplaneHead == NULL)
	{
		return;
	}

	Airplane *pre = airplaneHead;
	Airplane *p = airplaneHead->next;

	while (p)
	{
		if (p->time.year < y ||
		        (p->time.year == y && (p->time.month < m ||
		                               (p->time.month == m && p->time.day < d))))
		{
			pre->next = p->next;
			free(p);
			p = pre->next;
		}

		else
		{
			pre = p;
			p = p->next;
		}
	}

}
//推荐类似航班format TODO（有问题，运行到自身就结束了，需处理：已解决）
static int Recommend(Airplane* curair,Airplane* airplaneHead)
{
	int flag = 0;
	fprintf (stdout,"加载中，请稍后~~\n");
	system("pause");

	N_air *quq;
	quq = airplaneHead->next;

	fprintf (stdout,"\n-----------------START----------------\n");
	printf("同一路线的航班：\n\n");
//	system("pause");
	Sleep (800);
	printf("航班号\t起飞城市\t抵达城市\t所属公司\t所属型号\t起飞-抵达时间\t\t");
	printf("折扣票价(元)\t剩余票量\n");
	while (quq != NULL )
	{
		if (0 == strcmp(curair->flightNumber,quq->flightNumber)) //防止输出与输入相同的航班
		{
			quq = quq->next;
			continue ;
		}

		if(!strcmp(quq->startCity,curair->startCity) && !strcmp(quq->endCity,curair->endCity))
		{
			flag = 1;
			Display(quq);
		}

		quq = quq->next;


	}

	printf("++++++++++++++++++++EOF++++++++++++++++++++++-");
	printf("\n\n");

	if (flag == 0)
	{
		fprintf (stdout,"没有类似同一路线航线，抱歉\n");
		fprintf (stdout,"-----------------EOF----------------\n\n");
		system("pause");
		return 0;
	}

	quq = airplaneHead->next;
	flag = 0;//

	fprintf (stdout,"\n-----------------START----------------\n");
	printf("同一路线,相近时间的航班：\n");
//	system("pause");
	Sleep (800);
	while (quq != NULL )
	{
		if (0 == strcmp(curair->flightNumber,quq->flightNumber)) //防止输出与输入相同的航班
		{
			quq = quq->next;
			continue ;
		}

		if(quq->time.year == curair->time.year && quq->time.month == curair->time.month
		        &&
		        quq->time.day == curair->time.day
		        &&
		        !strcmp(quq->startCity,curair->startCity) && !strcmp(quq->endCity,curair->endCity))
		{
			flag = 1;
			Display(quq);
		}

		quq = quq->next;

	}

	printf("+++++++++++++++++++++EOF++++++++++++++++++++++-");
	printf("\n\n");


	if (flag == 0)
	{
		fprintf (stdout,"没有类似地点时间的航线，抱歉\n");
		fprintf (stdout,"-----------------EOF----------------\n\n");

	}

	system("pause");

	return 0;

}
//优化版字符串输入函数
char * s_gets(char * st,int n)
{
	char *re_val;
	int i = 0;

	re_val = fgets (st,n,stdin);
	if (re_val)
	{
		while (st[i] !='\n' && st[i] != '\0')
			i++;

		if(st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return re_val;
}

# endif